package com.czkevin.cifjiaocheng.cif;

import com.czkevin.cifjiaocheng.SoundUtil;
import de.tr7zw.nbtapi.NBTCompound;
import com.czkevin.cifaddon.blockentity.BaseBlockWithEntity;
import com.czkevin.cifaddon.blockentity.CustomBlockEntity;
import com.czkevin.cif.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.entity.Item;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

public class SepticTank extends BaseBlockWithEntity {
    @Override
    public String getDisplayName(NBTCompound nbtCompound) {
        return "\u00a76\u00a7l迷你化粪池";
    }

    @Override
    public Material getModelMaterial() {
        return Material.DIAMOND;
    }

    @Override
    public int getCustomModelData(CIFItemInstance instance) {
        return 114514103;
    }

    @Override
    public CustomBlockEntity onLoad(CIFItemInstance cifItemInstance, Location location) {
        return new SepticTankEntity(location, cifItemInstance);
    }

    /**
     * 化粪池实体
     */
    private static class SepticTankEntity extends CustomBlockEntity {
        public SepticTankEntity(Location location, CIFItemInstance state) {
            super(location, state);
        }

        private long last_check = System.currentTimeMillis();
        @Override
        public void doTick() {
            if(System.currentTimeMillis() - last_check < 2000L) return;
            last_check = System.currentTimeMillis();

            // 检查周围的Shit
            Location location = getLocation();
            location.getWorld().getNearbyEntities(location, 1.5d, 1.5d, 1.53)
                .stream().filter(e -> Item.class.isAssignableFrom(e.getClass())).filter(
                e -> {
                    // 检查是不是ShitOre
                    ItemStack i = ((Item) e).getItemStack();
                    if(i.getType() == Material.AIR) return false;
                    CIFItem type = CustomItemFramework.getCIFTypeFromItem(i);
                    return type != null && ShitOre.class.isAssignableFrom(type.getClass());
                }).forEach(e -> processEntity((Item) e));
        }

        private void processEntity(Item e) {
            ItemStack i = e.getItemStack();
            if(i.getAmount() > 1) {
                i.setAmount(i.getAmount() - 1);
                e.setItemStack(i);
            } else {
                e.remove();
            }

            dropShit();
        }

        private void dropShit() {
            Location location = getLocation();
            World world = location.getWorld();
            // 播放声音
            world.playSound(location, "jiaocheng.heng.random", SoundCategory.BLOCKS, 1.0f, SoundUtil.randomPitch());

            // 掉落物品
            world.dropItemNaturally(
                location,
                ItemRegister.get("Shit").generateItemStack(null, false)
            );
        }

        @Override
        public void initialize() { }
    }
}
